package com.emex.app.stuff.admin;

import com.emex.app.shared.data.CostType;
import com.emex.app.stuff.gui.CostTypeDialog;
import com.emex.app.stuff.gui.model.CostTypeTableModel;
import com.emex.app.stuff.provider.CostDataProvider;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.WindowManager;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//com.emex.app.stuff.admin//CostType//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "CostTypeTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_NEVER
)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "com.emex.app.stuff.admin.CostTypeTopComponent")
@ActionReference(path = "Menu/Администрирование" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_CostTypeAction",
        preferredID = "CostTypeTopComponent"
)
@Messages({
    "CTL_CostTypeAction=Статьи расходов",
    "CTL_CostTypeTopComponent=Статьи расходов",
    "HINT_CostTypeTopComponent=Статьи расходов"
})
public final class CostTypeTopComponent extends TopComponent {
    
    private CostTypeTableModel costTypeTableModel;

    public CostTypeTopComponent() {
        initComponents();
        setName(Bundle.CTL_CostTypeTopComponent());
        setToolTipText(Bundle.HINT_CostTypeTopComponent());
        customInit();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonsPanel = new javax.swing.JPanel();
        editCostTypeButton = new javax.swing.JButton();
        deleteCostTypeButton = new javax.swing.JButton();
        addNewCostTypeButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        costTypeTable = new org.jdesktop.swingx.JXTable();

        buttonsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        org.openide.awt.Mnemonics.setLocalizedText(editCostTypeButton, org.openide.util.NbBundle.getMessage(CostTypeTopComponent.class, "CostTypeTopComponent.editCostTypeButton.text")); // NOI18N
        editCostTypeButton.setEnabled(false);
        editCostTypeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editCostTypeButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(deleteCostTypeButton, org.openide.util.NbBundle.getMessage(CostTypeTopComponent.class, "CostTypeTopComponent.deleteCostTypeButton.text")); // NOI18N
        deleteCostTypeButton.setEnabled(false);
        deleteCostTypeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteCostTypeButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(addNewCostTypeButton, org.openide.util.NbBundle.getMessage(CostTypeTopComponent.class, "CostTypeTopComponent.addNewCostTypeButton.text")); // NOI18N
        addNewCostTypeButton.setToolTipText(org.openide.util.NbBundle.getMessage(CostTypeTopComponent.class, "CostTypeTopComponent.addNewCostTypeButton.toolTipText")); // NOI18N
        addNewCostTypeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNewCostTypeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout buttonsPanelLayout = new javax.swing.GroupLayout(buttonsPanel);
        buttonsPanel.setLayout(buttonsPanelLayout);
        buttonsPanelLayout.setHorizontalGroup(
            buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addNewCostTypeButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editCostTypeButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(deleteCostTypeButton)
                .addContainerGap(79, Short.MAX_VALUE))
        );
        buttonsPanelLayout.setVerticalGroup(
            buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonsPanelLayout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addGroup(buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addNewCostTypeButton)
                    .addComponent(editCostTypeButton)
                    .addComponent(deleteCostTypeButton))
                .addContainerGap())
        );

        costTypeTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                costTypeTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(costTypeTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(buttonsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(buttonsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void editCostTypeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editCostTypeButtonActionPerformed
        CostTypeDialog dialog = new CostTypeDialog(WindowManager.getDefault().getMainWindow(), true);
        dialog.setCostType(getCostTypeSelection());
        dialog.setCostTypeTableModel(costTypeTableModel);
        dialog.setLocationRelativeTo(null);
        dialog.setTitle("Редактировать статью расхода");
        dialog.setVisible(true);
    }//GEN-LAST:event_editCostTypeButtonActionPerformed

    private void deleteCostTypeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteCostTypeButtonActionPerformed
        CostType selection = getCostTypeSelection();
        CostDataProvider.getInstance().deleteCostTypeByPrimaryKey(selection.getId());
        costTypeTableModel.dataRemoved(selection);
    }//GEN-LAST:event_deleteCostTypeButtonActionPerformed

    private void addNewCostTypeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNewCostTypeButtonActionPerformed
        CostTypeDialog dialog = new CostTypeDialog(WindowManager.getDefault().getMainWindow(), true);
        dialog.setCostTypeTableModel(costTypeTableModel);
        dialog.setLocationRelativeTo(null);
        dialog.setTitle("Добавить новую статью расходов");
        dialog.setVisible(true);
    }//GEN-LAST:event_addNewCostTypeButtonActionPerformed

    private void costTypeTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_costTypeTableMouseClicked
        if (evt.getClickCount() == 2) {
            editCostTypeButtonActionPerformed(null);
        }
    }//GEN-LAST:event_costTypeTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addNewCostTypeButton;
    private javax.swing.JPanel buttonsPanel;
    private org.jdesktop.swingx.JXTable costTypeTable;
    private javax.swing.JButton deleteCostTypeButton;
    private javax.swing.JButton editCostTypeButton;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    protected CostType getCostTypeSelection() {
        if (costTypeTable.getSelectedRow() > -1) {
            CostType costType = (CostType) costTypeTable
                    .getValueAt(costTypeTable.getSelectedRow(), -1);
            return costType;
        }
        return null;
    }

    protected void customInit() {
        costTypeTableModel = new CostTypeTableModel();
        costTypeTable.setModel(costTypeTableModel);
        costTypeTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int row = costTypeTable.getSelectedRow();
                if ((row >= 0) && (row < costTypeTable.getRowCount())) {
                    editCostTypeButton.setEnabled(true);
                    deleteCostTypeButton.setEnabled(true);

                } else {
                    editCostTypeButton.setEnabled(false);
                    deleteCostTypeButton.setEnabled(false);
                }
            }
        });
    }
}
